package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserStage3Mapper;
import com.ruoyi.system.domain.SysUserStage3;
import com.ruoyi.system.service.ISysUserStage3Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 发展对象的确定和考核阶段Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserStage3ServiceImpl implements ISysUserStage3Service
{
    @Autowired
    private SysUserStage3Mapper sysUserStage3Mapper;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询发展对象的确定和考核阶段
     *
     * @param id 发展对象的确定和考核阶段主键
     * @return 发展对象的确定和考核阶段
     */
    @Override
    public SysUserStage3 selectSysUserStage3ById(Long id)
    {
        return sysUserStage3Mapper.selectSysUserStage3ById(id);
    }

    /**
     * 查询发展对象的确定和考核阶段列表
     *
     * @param sysUserStage3 发展对象的确定和考核阶段
     * @return 发展对象的确定和考核阶段
     */
    @Override
    public List<SysUserStage3> selectSysUserStage3List(SysUserStage3 sysUserStage3)
    {
        return sysUserStage3Mapper.selectSysUserStage3List(sysUserStage3);
    }

    /**
     * 新增发展对象的确定和考核阶段
     *
     * @param sysUserStage3 发展对象的确定和考核阶段
     * @return 结果
     */
    @Override
    public int insertSysUserStage3(SysUserStage3 sysUserStage3)
    {
        sysUserStage3.setCreateTime(DateUtils.getNowDate());
        return sysUserStage3Mapper.insertSysUserStage3(sysUserStage3);
    }

    /**
     * 修改发展对象的确定和考核阶段
     *
     * @param sysUserStage3 发展对象的确定和考核阶段
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserStage3(SysUserStage3 sysUserStage3)
    {
        String createBy = sysUserStage3.getCreateBy();
        String userName = sysUserStage3.getDevelopUserName();
        sysUserStage3.setUpdateTime(DateUtils.getNowDate());
        SysUserStage3 sysUserStage = sysUserStage3Mapper.selectSysUserStage3ByDevelopId(sysUserStage3.getSysUserDevelopId());
        Date step7ApproveTime = sysUserStage3.getStep7ApproveTime();
        Date step8DevelopmentTime = sysUserStage3.getStep8DevelopmentTime();
        Long step9User1Id = sysUserStage3.getStep9User1Id();
        Date step10CheckFinishTime = sysUserStage3.getStep10CheckFinishTime();
        Date step11TrainTime = sysUserStage3.getStep11TrainTime();
        SysUserDevelop sysUserDevelop = new SysUserDevelop();
        sysUserDevelop.setId(sysUserStage.getSysUserDevelopId());
        //父归档文件
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(sysUserStage3.getSysUserDevelopId()));
        if(!ObjectUtils.isEmpty(step7ApproveTime)){
            sysUserStage3.setStep7Status("1");
            sysUserStage3.setStep7FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("确定发展对象阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段7归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("确定发展对象阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage3.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段7文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("确定发展对象人选会议记录");
            deptFileArchivedVo2.setFileUrl(sysUserStage3.getStep7File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step8DevelopmentTime)){
            sysUserStage3.setStep8Status("1");
            sysUserStage3.setStep8FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("报上级党委备案阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段8归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("报上级党委备案阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage3.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段8文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("党员发展对象备案报告");
            deptFileArchivedVo2.setFileUrl(sysUserStage3.getStep8File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("列为党员发展对象批复");
            deptFileArchivedVo3.setFileUrl(sysUserStage3.getStep8File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);

            DeptFileArchived deptFileArchivedVo4 = new DeptFileArchived();
            deptFileArchivedVo4.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo4.setCreateBy(createBy);
            deptFileArchivedVo4.setCreateName(userName);
            deptFileArchivedVo4.setFileType("1");
            deptFileArchivedVo4.setFileName("确定党员发展对象公示");
            deptFileArchivedVo4.setFileUrl(sysUserStage3.getStep8File3());
            deptFileArchivedVo4.setParentId(archivedId);
            deptFileArchivedVo4.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo4);

            DeptFileArchived deptFileArchivedVo5 = new DeptFileArchived();
            deptFileArchivedVo5.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo5.setCreateBy(createBy);
            deptFileArchivedVo5.setCreateName(userName);
            deptFileArchivedVo5.setFileType("1");
            deptFileArchivedVo5.setFileName("确定发展对象公示结果确认表");
            deptFileArchivedVo5.setFileUrl(sysUserStage3.getStep8File4());
            deptFileArchivedVo5.setParentId(archivedId);
            deptFileArchivedVo5.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo5);

            DeptFileArchived deptFileArchivedVo6 = new DeptFileArchived();
            deptFileArchivedVo6.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo6.setCreateBy(createBy);
            deptFileArchivedVo6.setCreateName(userName);
            deptFileArchivedVo6.setFileType("1");
            deptFileArchivedVo6.setFileName("发展对象情况登记表");
            deptFileArchivedVo6.setFileUrl(sysUserStage3.getStep8File5());
            deptFileArchivedVo6.setParentId(archivedId);
            deptFileArchivedVo6.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo6);

        }else if(!ObjectUtils.isEmpty(step9User1Id)){
            sysUserStage3.setStep9Status("1");
            sysUserStage3.setStep9FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("确定入党介绍人阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段9归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("确定入党介绍人阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage3.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段9文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage3.getStep9File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }else if(!ObjectUtils.isEmpty(step10CheckFinishTime)){
            sysUserStage3.setStep10Status("1");
            sysUserStage3.setStep10FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("进行政治审查阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段10归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("进行政治审查阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage3.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段10文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("发展党员政治审查函");
            deptFileArchivedVo2.setFileUrl(sysUserStage3.getStep10File1());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

            DeptFileArchived deptFileArchivedVo3 = new DeptFileArchived();
            deptFileArchivedVo3.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo3.setCreateBy(createBy);
            deptFileArchivedVo3.setCreateName(userName);
            deptFileArchivedVo3.setFileType("1");
            deptFileArchivedVo3.setFileName("政治审查情况报告");
            deptFileArchivedVo3.setFileUrl(sysUserStage3.getStep10File2());
            deptFileArchivedVo3.setParentId(archivedId);
            deptFileArchivedVo3.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo3);
        }else if(!ObjectUtils.isEmpty(step11TrainTime)){
            sysUserStage3.setStep11Status("1");
            sysUserStage3.setStep11FinishTime(DateUtils.getNowDate());
            sysUserDevelop.setDevelopStage("开展集中培训阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);

            //创建阶段11归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("开展集中培训阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage3.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段11文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("相关资料");
            deptFileArchivedVo2.setFileUrl(sysUserStage3.getStep11File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage3.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        sysUserStage3.setId(sysUserStage.getId());
        return sysUserStage3Mapper.updateSysUserStage3(sysUserStage3);
    }

    /**
     * 删除发展对象的确定和考核阶段信息
     *
     * @param id 发展对象的确定和考核阶段主键
     * @return 结果
     */
    @Override
    public int deleteSysUserStage3ById(Long id)
    {
        return sysUserStage3Mapper.deleteSysUserStage3ById(id);
    }

    /**
     * 删除入党申请人阶段信息
     *
     * @param userDevelopId 入党申请人阶段主键s
     * @return 结果
     */
    @Override
    public int deleteSysUserStage3ByUserDevelopId(Long userDevelopId){
        return sysUserStage3Mapper.deleteSysUserStage3ByUserDevelopId(userDevelopId);
    }

    /**
     * 查询入党申请人阶段
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 入党申请人阶段
     */
    @Override
    public SysUserStage3 selectSysUserStage3ByDevelopId(Long userDevelopId){
        return sysUserStage3Mapper.selectSysUserStage3ByDevelopId(userDevelopId);
    }
}
